﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AddressableAssets;
using UnityEngine.ResourceManagement.AsyncOperations;
using UnityEngine.ResourceManagement.ResourceProviders;
//10.加载：通过地址加载
public class Example3 : MonoBehaviour
{


    public string characterTexName = "";
    public AsyncOperationHandle<Texture> texHandle;

    public string cubeName = "";
    public AsyncOperationHandle<GameObject> cubeHandle;

    public string sceneName = "";
    private AsyncOperationHandle<SceneInstance> sceneHandle;
    private void Start()
    {
        
    }
    private void OnGUI()
    {

        ////////////////////////////////加载贴图/////////////////////////////
        if (GUILayout.Button("加载贴图 "))
        {
            texHandle = Addressables.LoadAssetAsync<Texture>(characterTexName);
            texHandle.Completed += args =>
            {
                Debug.Log("加载"+args.Result.name);
            };
        }


        if (GUILayout.Button("释放贴图"))
        {
            Addressables.Release(texHandle);
        }
        /////////////////////////////////实例化GameObject////////////////////////////
        if (GUILayout.Button("实例化 prefab "))
        {
            cubeHandle = Addressables.InstantiateAsync(cubeName);
            cubeHandle.Completed += args =>
            {
                Debug.Log("加载" + args.Result.name);
            };
        }


        if (GUILayout.Button("释放 GameObject"))
        {
            Addressables.ReleaseInstance(cubeHandle);
        }
        //////////////////////////////////加载场景/////////////////
        if (GUILayout.Button("加载场景 "))
        {
            //如果要LoadSceneMode.Single方式，就没必要用这个方法，用SceneManager.LoadScene就可以了
            sceneHandle = Addressables.LoadSceneAsync(sceneName, UnityEngine.SceneManagement.LoadSceneMode.Additive);

            sceneHandle.Completed += args =>
            {
                Debug.Log("加载" + args.Result.Scene.name);
            };
        }


        if (GUILayout.Button("释放 Scene"))
        {
            //console有异常
            Addressables.UnloadSceneAsync(sceneHandle);
        }

    }





}
